<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/12/1
 * Time: 上午11:46
 */
class SceCarController extends BaseController {
    /*
     * 获取车辆最近的一次检测记录
     * */
    public function get_order_detection_history(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'car_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $car_id=Input::get("car_id");
            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);

            if(is_null($engineer_data)){
                
                break;
            }
            $orders=Orders::OrderBy('created_at','desc')
                ->where('user_car_id',$car_id)
                ->whereIn('status',array(1018,1017,1217,1317,1417))
                ->first();

            if(is_null($orders)){
                // $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
                $response = Lang::get("sce_error.detection_error_2001");
                break;
            }else{
                $de=OrderDetection::getOrderDetection($orders->order_id);
                $order_info=array("car_brand_id"=>$orders->car_brand_id,"car_brand_name"=>$orders->car_brand_name,"car_series_id"=>$orders->car_series_id,"car_series_name"=>$orders->car_series_name,"car_model_id"=>$orders->car_model_id,"car_model_name"=>$orders->car_model_name,"mileage"=>$orders->mileage);
                $de["order_info"]=$order_info;
                $response = array('code' => 0, 'msg' => 'ok', 'result' => $de);
            }

        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "get_order_detection_history", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }



    /*
     * 查询车辆型号
     * */
    public function models(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'brand' => 'required',
                'series' => 'required',
                'year' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $brand = Input::get('brand');
            $series = Input::get('series');
            $year = Input::get('year');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            //获取品牌车辆的厂商
            $models = DataCarStandardInfo::where('brand_name_id',$brand)
                ->where('series_name_id',$series)
                ->where('model_year',$year)
                ->where('status',1)
                ->select(array('id','model_name as model'))
                ->distinct()
                ->get()->toArray();
            $count = count($models);
            $result = array(
                'count' => $count,
                'models' => $models
            );

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "models", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询这辆年款
     * */
    public function years(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'brand' => 'required',
                'series' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $brand = Input::get('brand');
            $series = Input::get('series');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            //获取品牌车辆的厂商
            $years = DataCarStandardInfo::where('brand_name_id',$brand)
                ->where('series_name_id',$series)
                ->where('status',1)
                ->select(array('model_year as year'))
                ->distinct()
                ->get()->toArray();
            $count = count($years);
            $result = array(
                'count' => $count,
                'years' => $years
            );

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "years", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询车辆系列
     * */
    public function series(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'brand' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $brand = Input::get('brand');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            //获取品牌车辆的厂商
            $factories = DataCarStandardInfo::where('brand_name_id',$brand)->where('status', 1)
                ->select(array('series_shop_id','series_shop'))
                ->distinct()
                ->get();
            $car_factories = array();
            if(!is_null($factories)){
                foreach ($factories as $_factory) {
                    $series = DataCarStandardInfo::where('brand_name_id',$brand)
                        ->where('series_shop_id',$_factory->series_shop_id)
                        ->select(array('series_name_id as id','series_name as name'))
                        ->distinct()
                        ->get()->toArray();
                    $factory = array(
                        'factory' => $_factory->series_shop,
                        'count' => count($series),
                        'series' => $series
                    );
                    array_push($car_factories,$factory);
                }
            }
            $result = array(
                'count' => count($car_factories),
                'factories' => $car_factories
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "series", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询车辆品牌
     * */
    public function brands(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            //获取车辆品牌信息
            $brands = DataCarBrand::select(array('id','initial','name','icon'))->where('status', 1)
                ->get()->toArray();
            $count = count($brands);
            $result = array(
                'count' => $count,
                'base_icon_url' => Config::get('car.base_icon_url'),
                'brands' => $brands
            );

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "brands", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询车辆订单
     * */
    /**
     * @Author:      lyt
     * @DateTime:    2016-02-14 10:18:07
     * @Description: fix 修改接口 增加接口返回的信息
     *  修改 查询的车辆为 最新的认证的车辆信息
     *  添加 添加该车牌号 在登录技师门店的 正在进行的订单列表
     */
    public function search(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'car_no' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $car_no = Input::get('car_no');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            //技师端根据车牌号查询认证车辆
            $car = UserCarInfo::where('car_no',$car_no)
                ->where('status',1)
                ->where('is_lock',1)
                ->select(array('id','vin_code','car_model_id','note','mileage'))->orderBy("created_at","desc")
                ->first();
            if(!is_null($car)){
                $car_model = DataCarStandardInfo::where('id',$car->car_model_id)->first();
                $result = array(
                    'id' => $car->id,
                    'brand_id' => $car_model->brand_name_id,
                    'brand_name' => $car_model->brand_name,
                    'factory_id' => $car_model->series_shop_id,
                    'factory_name' => $car_model->series_shop,
                    'series_id' => $car_model->series_name_id,
                    'series_name' => $car_model->series_name,
                    'year' => $car_model->model_year,
                    'model_id' => $car_model->id,
                    'model_name' => $car_model->model_name,
                    'vin' => $car->vin_code,
                    'note' => $car->note,
                    'mileage' => $car->mileage
                );
            }else{
                $result = array(
                    'id' => 0,
                    'brand_id' => null,
                    'brand_name' => null,
                    'factory_id' => null,
                    'factory_name' => null,
                    'series_id' => null,
                    'series_name' => null,
                    'year' => null,
                    'model_id' => null,
                    'model_name' => null,
                    'vin' => null,
                    'note' => null,
                    'mileage' => null
                );
            }
            $mp=MaintenancePark::leftJoin("orders","orders.order_id","=","maintenance_park.order_id")->where("maintenance_park.shop_id",$engineer_data->type_id)->where("maintenance_park.status",4)->where("orders.car_no",$car_no)->where("orders.engineer_id",$engineer_data->id)->select(array("maintenance_park.order_id"))->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>array("car_info"=>$result,"order_info"=>$mp));
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "search", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}